package tree;

import offer.TreeNode;

public class leetCode111_minDepth {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        return build(root);
    }

    public int build(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            return 1;
        }
        int ans = Integer.MAX_VALUE;
        if(root.left != null){
            ans = Math.min(build(root.left),ans);
        }
        if(root.right != null){
            ans = Math.min(build(root.right),ans);
        }
        return ans + 1;
    }
}
